﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cotrobaie.UsersWebService;

namespace Cotrobaie
{
    public partial class Bookmarks : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ButtonStatistics.Attributes.Add("onMouseOut", "this.src='/images/comm.png'");
            ButtonStatistics.Attributes.Add("onMouseOver", "this.src='/images/ocomm.png'");
            ButtonDescr.Attributes.Add("onMouseOut", "this.src='/images/recc.png'");
            ButtonDescr.Attributes.Add("onMouseOver", "this.src='/images/orecc.png'");
            ButtonComments.Attributes.Add("onMouseOut", "this.src='/images/acc.png'");
            ButtonComments.Attributes.Add("onMouseOver", "this.src='/images/oacc.png'");

            //if user logged
            if (Session["UserID"] == null)
                Response.Redirect("~/Home.aspx");
            else
            {
                if (!Page.IsPostBack) Session["BookmarksCurrPage"] = null;

                using (UsersSoapClient serviciu = new UsersSoapClient("UsersSoap"))
                {
                    var result = serviciu.GetUserBookmarks((int)Session["UserID"], Session["UserUsername"].ToString(), Session["UserParola"].ToString(),0,1);
                    if (result.Produse.Count() == 0)
                    {
                        Literal1.Text = "Bookmark list is empty.";
                        ButtonNextPage.Visible = false;
                        ButtonPrevPage.Visible = false;
                    }
                    else
                    {
                        PlaceHolder1.Controls.Clear();
                        Table table = new Table();
                        PlaceHolder1.Controls.Add(table);
                        // header
                        TableRow tr = new TableRow();

                        TableCell tc = new TableCell();
                        Label lab = new Label();
                       
                       
                      

                        //actual row
                        int intrariPagina = 3;
                        if (Session["BookmarksCurrPage"] == null) Session["BookmarksCurrPage"] = 0;
                        var bookmarks = serviciu.GetUserBookmarks((int)Session["UserID"], Session["UserUsername"].ToString(), Session["UserParola"].ToString(), (int)Session["BookmarksCurrPage"], intrariPagina);

                        if (bookmarks.Produse.Count() == 0 && (int)Session["BookmarksCurrPage"] > 0)
                        {
                            bookmarks = serviciu.GetUserBookmarks((int)Session["UserID"], Session["UserUsername"].ToString(), Session["UserParola"].ToString(), (int)Session["BookmarksCurrPage"] - 1, intrariPagina);
                            Session["BookmarksCurrPage"] = (int)Session["BookmarksCurrPage"] - 1;
                            ButtonNextPage.Enabled = false;
                            ButtonNextPage.CssClass = "button3";
                        }
                        if (bookmarks.Produse.Count() < intrariPagina) { ButtonNextPage.Enabled = false; ButtonNextPage.CssClass = "button3"; }
                        if ((int)Session["BookmarksCurrPage"] == 0) { ButtonPrevPage.Enabled = false; ButtonPrevPage.CssClass = "button3"; }
                        LabelCurrentPage.Text = ((int)Session["BookmarksCurrPage"] + 1).ToString();
                        int i = 0;
                        foreach (var b in bookmarks.Produse)
                        {
                            i++;
                            tr = new TableRow();

                            tc = new TableCell();
                            HyperLink image = new HyperLink();
                            image.CssClass = "smallThumb";
                            image.Text = "<img class=\"smallThumb\" src=\"" + (b.imageLarge != null && b.imageLarge.Length > 0 ? b.imageLarge : (b.imageMedium != null && b.imageMedium.Length > 0 ? b.imageMedium : (b.imageSmall != null && b.imageSmall.Length > 0 ? b.imageSmall : "images/img.JPG"))) + "\" style=\"height:160px;\" />";
                            image.NavigateUrl = b.linkSite;

                            tc.Controls.Add(image);
                            tr.Cells.Add(tc);
                            table.Rows.Add(tr);

                            tc = new TableCell();
                            lab = new Label();
                            lab.Text = "&nbsp;";
                            tc.Controls.Add(lab);
                            tr.Cells.Add(tc);
                            table.Rows.Add(tr);

                            tc = new TableCell();
                            HyperLink hp = new HyperLink();
                            hp.NavigateUrl = b.linkSite;
                            hp.CssClass = "blgrlink";
                            hp.Text = b.name;
                            tc.Controls.Add(hp);
                            tc.Width = 150;
                            tr.Cells.Add(tc);
                            table.Rows.Add(tr);

                            tc = new TableCell();
                            lab = new Label();
                            lab.Text = "$" + b.Price.ToString();
                            tc.Controls.Add(lab);
                            tc.Width = 50;
                            tr.Cells.Add(tc);
                            table.Rows.Add(tr);

                            tc = new TableCell();
                            hp = new HyperLink();
                            hp.NavigateUrl = b.linkSite;
                            if (b.linkAmazon != null)
                            {
                                hp.NavigateUrl = b.linkAmazon;
                                hp.Text = "Amazon";
                            }
                            else
                            {
                                hp.NavigateUrl = b.linkEbay;
                                hp.Text = "Ebay";
                            }
                            hp.CssClass = "blgrlink";
                            tc.Controls.Add(hp);
                            tc.Width = 90;
                            tr.Cells.Add(tc);
                            table.Rows.Add(tr);


                            tc = new TableCell();
                            Button but = new Button();
                            but.Text = "Remove";
                            but.CssClass = "bigbutton";
                            but.Click += onClickRemove;
                            but.CommandName = "remove";
                            but.CommandArgument = b.ID.ToString();

                            tc.Controls.Add(but);
                            tr.Cells.Add(tc);
                            table.Rows.Add(tr);


                            //empty row
                            tr = new TableRow();
                            tc = new TableCell();
                            lab = new Label();
                            lab.Text = "&nbsp;";
                            tc.Controls.Add(lab);
                            tr.Cells.Add(tc);
                            table.Rows.Add(tr);
                        }


                    }
               }
            }
        }

        public void onClickRemove(object sender, EventArgs e)
        {
            using (UsersSoapClient serviciu = new UsersSoapClient("UsersSoap"))
            {
                long produs;
                Button but = (Button)sender;
                produs = long.Parse(but.CommandArgument);
                var result = serviciu.RemoveUserBookmark((int)Session["UserID"], Session["UserUsername"].ToString(), Session["UserParola"].ToString(), produs);
                if (result.Success)
                    Page_Load(null, null);
            }
        }

        protected void ButtonPrevPage_Click(object sender, EventArgs e)
        {
            if (Session["BookmarksCurrPage"] != null)
                Session["BookmarksCurrPage"] = (int)Session["BookmarksCurrPage"] - 1;
            else Session["BookmarksCurrPage"] = 0;
            ButtonNextPage.Enabled = true;
            ButtonNextPage.CssClass = "button2";
            Page_Load(null, null);
        }

        protected void ButtonNextPage_Click(object sender, EventArgs e)
        {
            if (Session["BookmarksCurrPage"] != null)
                Session["BookmarksCurrPage"] = (int)Session["BookmarksCurrPage"] + 1;
            else Session["BookmarksCurrPage"] = 1;
            ButtonPrevPage.Enabled = true;
            ButtonPrevPage.CssClass = "button2";
            Page_Load(null, null);
        }

        public void tabClick(object sender, EventArgs e)
        {
            ImageButton but = (ImageButton)sender;

            if (but.ID == "ButtonDescr")
            {
                Response.Redirect("~/Recommendations.aspx");
            }

            else if (but.ID == "ButtonComments")
            {
                Response.Redirect("~/MyAccount.aspx");
            }

            else if (but.ID == "ButtonStatistics")
            {
                Response.Redirect("~/Comments.aspx");
            }

        }

        protected void goHome(object sender, EventArgs e)
        {
            Response.Redirect("~/Home.aspx");
        }

    }
}